﻿//注册名字空间。

registerNamespace = function(namespacePath) {
    var rootObject = window;
    var namespaceParts = namespacePath.split('.');
    for (var i = 0;i < namespaceParts.length; i++) {
        var currentPart = namespaceParts[i];
        if (!rootObject[currentPart]) {
            rootObject[currentPart] = new Object();
        }
        rootObject = rootObject[currentPart];
    }
};

registerNamespace("dream");

if (!window.Class) {
	window.Class = {
		create : function() {
			return function() {
				this.initialize.apply(this, arguments);
			}
		}
	}
}

if (!Object.extend) {
	Object.extend = function(dest, source) {
		for (prop in source) {
			dest[prop] = source[prop];
		}
		return dest;
	}
}

Object.extend(String.prototype, {
	Trim : function() {
		return this.replace(/(^\s*) | (\s*$)/g, "");
	}, 
	lTrim : function() {
		return this.replace(/(^\s*)/g, "");
	},
	rTrim : function() {
		return this.replace(/(\s*$)/g, "");
	}
})

dream.String = {
	isNullOrEmpty : function(str) {
		if (null == str || "" == str) {
			return true;
		}
		else {
			return false;
		}
	}
};

//一个关于日期的自定义类
dream.DateInfo = Class.create();
dream.DateInfo.prototype = {
	initialize : function(date) {
		this._Date = date;
		if (date == "undefined") {
			this._Date = new Date();
		}
		this._Lunar = this._GetLunar(this._Date);
	},
	getYear : function() {
		return this._Date.getFullYear();
	},
	getLYear : function() {
		//获取农历年
		return this._Lunar.Year;
	},
	getMonth : function() {
		return this._Date.getMonth() + 1;
	},
	getLMonth : function() {
		//获取农历月
		return this._Lunar.Month;
	},
	getDate : function() {
		return this._Date.getDate();
	},
	getLDate : function() {
		//获取农历日
		return Math.floor(this._Lunar.Day);
	},
	getWeek : function() {
		var weekDays = new Array("<font color='#ff0000'>星期日</font>", "星期一", "星期二", "星期三", "星期四", "星期五", "<font color='#ff0000'>星期六</font>");
		return weekDays[this._Date.getDay()];
	},
	getHours : function() {
		return this._Date.getHours();
	},
	getMinites : function() {
		return this._Date.getMinites();
	},
	getGreeting : function() {
		var tempIndex = -1;
	var hour = this.getHours();
	if (hour >= 4 && hour <= 7) {
		tempIndex = 0;
	}
	else if (hour >= 8 && hour <= 11) {
		tempIndex = 1;
	}
	else if (hour >= 12 && hour <= 13) {
		tempIndex = 2;
	}
	else if (hour >= 14 && hour <= 18) {
		tempIndex = 3;
	}
	else if (hour >= 19 && hour <= 22) {
		tempIndex = 4;
	}
	else if (hour == 23 || (hour >= 0 && hour <= 3)) {
		tempIndex = 5;
	}
	else {
		tempIndex = -1;
	}

	var Greetings = new Array("早上好!", "上午好!", "中午好!", "下午好!", "晚上好!", "深夜好!");
	return Greetings[tempIndex];
	},
	toLocalString : function() {
		return this.getYear() + "年" + this.getMonth() + "月" + this.getDate() + "日";
	},
	toLLocalString : function() {
		var nStr1 = new Array('日','一','二','三','四','五','六','七','八','九','十', '十一', '十二');
		var nStr2 = new Array('初','十','廿','卅','　');
		var d = this.getLDate();
		var s;
		switch (d) {
			case 10:
				s = '初十'; break;
			case 20:
				s = '二十'; break;
			case 30:
				s = '三十'; break;
			default :
				s = nStr2[Math.floor(d/10)];
				s += nStr1[d%10];
				break;
		}
		s = nStr1[this.getLMonth()] + "月" + s;
		return(s);
	},
	_GetLunar : function(objDate) {
		//存放 着阴历信息，包括 .Year .Month .Day .isLeap .yearCyl .dayCyl .monCyl 等七个物件(/属性)
		var i, leap=0, temp=0;
		var baseDate = new Date(1900,0,31);
		var offset = (objDate - baseDate)/86400000;
		
		var me = new Object();
		me.dayCyl = offset + 40;
		me.monCyl = 14;
		
		for(i = 1900; i < 2050 && offset > 0; i++) {
			temp = this._lYearDays(i);
			offset -= temp;
			me.monCyl += 12;
		}
		
		if(offset < 0) {
			offset += temp;
			i--;
			me.monCyl -= 12;
		}
		
		me.Year = i;
		me.yearCyl = i - 1864;
		
		leap = this._leapMonth(i); //闰哪个月
		me.isLeap = false;
		
		for(i = 1; i < 13 && offset > 0; i++) {
			//闰月
			if((leap > 0) && (leap + 1 == i) && (false == me.isLeap)) {
				--i;
				me.isLeap = true;
				temp = this._leapDays(me.Year);
			}
			else {
				temp = this._monthDays(me.Year, i);
			}
			
			//解除闰月
			if((true == me.isLeap) && (leap+1 == i)) {
				me.isLeap = false;
			}
			
			offset -= temp;
			if(false == me.isLeap) {
				me.monCyl++;
			}
		}
		
		if((0 == offset) && (leap > 0) && (leap+1 == i)) {
			if(me.isLeap) {
				me.isLeap = false; 
			}
			else {
				me.isLeap = true;
				--i;
				--me.monCyl;
			}
		}
		
		if(offset < 0) {
			offset += temp;
			--i;
			--me.monCyl;
		}
		
		me.Month = i;
		me.Day = offset + 1;
		return me;
	},
	_lYearDays : function(y) {
		//传回农历 y年的总天数
		var i, sum = 348;
		for(i = 0x8000; i > 0x8; i >>= 1) {
			sum += (this._lunarInfo[y-1900] & i) ? 1 : 0;
		}
		return(sum + this._leapDays(y));
	},
	_leapDays :　function(y) {
		//传回农历 y年闰月的天数
		if(this._leapMonth(y)) {
			return((this._lunarInfo[y-1900] & 0x10000) ? 30 : 29);
		}
		else {
			return(0);
		}
	},
	_leapMonth : function(y) {
		//传回农历 y年闰哪个月 1-12 , 没闰传回 0
		return(this._lunarInfo[y-1900] & 0xf);
	},
	_monthDays : function(y,m) {
		//传回农历 y年m月的总天数
		return((this._lunarInfo[y-1900] & (0x10000 >> m)) ? 30 : 29 );
	},
	_lunarInfo : new Array(
		0x04bd8,0x04ae0,0x0a570,0x054d5,0x0d260,0x0d950,0x16554,0x056a0,0x09ad0,0x055d2,
		0x04ae0,0x0a5b6,0x0a4d0,0x0d250,0x1d255,0x0b540,0x0d6a0,0x0ada2,0x095b0,0x14977,
		0x04970,0x0a4b0,0x0b4b5,0x06a50,0x06d40,0x1ab54,0x02b60,0x09570,0x052f2,0x04970,
		0x06566,0x0d4a0,0x0ea50,0x06e95,0x05ad0,0x02b60,0x186e3,0x092e0,0x1c8d7,0x0c950,
		0x0d4a0,0x1d8a6,0x0b550,0x056a0,0x1a5b4,0x025d0,0x092d0,0x0d2b2,0x0a950,0x0b557,
		0x06ca0,0x0b550,0x15355,0x04da0,0x0a5d0,0x14573,0x052d0,0x0a9a8,0x0e950,0x06aa0,
		0x0aea6,0x0ab50,0x04b60,0x0aae4,0x0a570,0x05260,0x0f263,0x0d950,0x05b57,0x056a0,
		0x096d0,0x04dd5,0x04ad0,0x0a4d0,0x0d4d4,0x0d250,0x0d558,0x0b540,0x0b5a0,0x195a6,
		0x095b0,0x049b0,0x0a974,0x0a4b0,0x0b27a,0x06a50,0x06d40,0x0af46,0x0ab60,0x09570,
		0x04af5,0x04970,0x064b0,0x074a3,0x0ea50,0x06b58,0x055c0,0x0ab60,0x096d5,0x092e0,
		0x0c960,0x0d954,0x0d4a0,0x0da50,0x07552,0x056a0,0x0abb7,0x025d0,0x092d0,0x0cab5,
		0x0a950,0x0b4a0,0x0baa4,0x0ad50,0x055d9,0x04ba0,0x0a5b0,0x15176,0x052b0,0x0a930,
		0x07954,0x06aa0,0x0ad50,0x05b52,0x04b60,0x0a6e6,0x0a4e0,0x0d260,0x0ea65,0x0d530,
		0x05aa0,0x076a3,0x096d0,0x04bd7,0x04ad0,0x0a4d0,0x1d0b6,0x0d250,0x0d520,0x0dd45,
		0x0b5a0,0x056d0,0x055b2,0x049b0,0x0a577,0x0a4b0,0x0aa50,0x1b255,0x06d20,0x0ada0)
};

//支持在firefox下复制的拷贝节点内容函数(ff下会弹出对话框，需要点击allow同意修改其安全属性才能复制)
/*notes about security:
a cause of the tight security settings in mozilla you have to sign thejavascript to make it work another way is to change yourfirefox/mozilla settings
to do this add this line to your prefs.js file in your firefox/mozilla user profile directory
user_pref("signed.applets.codebase_principal_support", true);
or change it from within the browser with calling the "about:config" page
*/
function copyToClipBoard(obj) {
	var meintext = "";
	try
	{
		meintext = obj.innerHTML;
	}
	catch(err)
	{
		try
		{
			meintext = obj.value;
		}
		catch(err2)
		{
			alert('复制失败！;\n原因：您使用了不支持复制的节点');
			return false;
		}
	}
	if (window.clipboardData) 
	{
		// the IE-manier
		window.clipboardData.setData("Text", meintext);
		
		// waarschijnlijk niet de beste manier om Moz/NS te detecteren;
		// het is mij echter onbekend vanaf welke versie dit precies werkt:
	}
	else if (window.netscape) 
	{ 
		// dit is belangrijk maar staat nergens duidelijk vermeld:
		// you have to sign the code to enable this, or see notes below 
		netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
		
		// maak een interface naar het clipboard
		var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
		if (!clip)
			return false;
		
		// maak een transferable
		var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
		if (!trans)
			return false;
		
		// specificeer wat voor soort data we op willen halen; text in dit geval
		trans.addDataFlavor('text/unicode');
		
		// om de data uit de transferable te halen hebben we 2 nieuwe objecten 
		// nodig om het in op te slaan
		var str = new Object();
		var len = new Object();
		var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
		var copytext = meintext;
		str.data = copytext;
		trans.setTransferData("text/unicode", str, copytext.length * 2);
		var clipid = Components.interfaces.nsIClipboard;
		if (!clip)
			return false;
		clip.setData(trans, null, clipid.kGlobalClipboard);
	}
	return false;
}